package com.lgj.demo0812.TestDemo;/**
*@author lizy51
*@date 2022-10-17 15:45
*/

import com.lgj.demo0812.pojo.User;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhanghuilong
 * @desc
 * @since 2018/06/12
 */
public class RefectField {

    public static void main(String[] args) throws Exception {

        User goodsVO = new User("1","akk","1**","flag",null);
        Class<?> aClass = User.class;
        Field[] fields = aClass.getDeclaredFields();
        Map<Object, Object> map = new HashMap<>();
        for (Field field  : fields){
            map.put(field.getName() , getResult(field.getName() , goodsVO));
        }

        System.out.println(map);
    }


    private static Object getResult(Object fieldName, User goodsVO) {
        try {
            Class<?> aClass = goodsVO.getClass();
            Field declaredField = aClass.getDeclaredField(fieldName.toString());
            declaredField.setAccessible(true);
            PropertyDescriptor pd = new PropertyDescriptor(declaredField.getName(), aClass);
            Method readMethod = pd.getReadMethod();

            return readMethod.invoke(goodsVO);
        } catch (Exception e) {
            e.printStackTrace();
        } /*catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }*/
        return null;
    }
}